<?php
namespace Home\Controller;
use Lib\Alidayu\SendSMS;
class AuditController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("audit");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
		$db_member = M("member");
		$db_data = M("data");
		$db_broadcasttime = M("broadcasttime");
	}


	//主播列表

	//注册待审核列表
	public function audit_list()
	{
		$this->p = I('get.p')?I('get.p'):1;
		$db_member = M("member m");
		$db_data = M("data");
		$this->audiotype = I('get.audiotype')==1?I('get.audiotype'):0;
		$where = "status=8 ";
		if ($this->audiotype==1) {$where .= " and d.introduction_audio_type=0 and d.introduction_audio !='' ";}
		$count = $db_member->join('__DATA__ d ON m.uuid = d.uuid')->where($where)->field('m.*')->count();
		$p = getpage($count, 20);
		$userinfo = $db_member->join('__DATA__ d ON m.uuid = d.uuid')->where($where)->field('m.*')->limit($p->firstRow, $p->listRows)->order("creationtime")->select();
		foreach ($userinfo as $key => &$value) {
			$value['invitename'] = "无";
			$invite = M("invite")->where(array('uuid'=>$value['uuid']))->find();
			$value['inviteid'] = M("invite")->where(array('uuid'=>$value['uuid']))->find()['inviteid'];
			if ($value['inviteid']) {
				$value['invitename'] = M("member")->where(array('uuid'=>$value['inviteid']))->find()['nickname'];
			}else{
				$value['inviteid'] = "无";
			}
		}
		$this->assign('userinfo',$userinfo);
		$this->assign('page', $p->show());
		$this->display();
	}

	//注册待审核详情
	public function audit_message()
	{
		$uid = I('get.uid');
		$this->p = I('get.p');
		$this->audiotype = I('get.audiotype');
		$db_member = M("member");
		$db_broadcasttime = M("broadcasttime");
		$db_data = M("data");
		$userdata = $db_member->where(array('status'=>8,'id'=>$uid))->find();
		$userdata['articles'] = explode(",", $userdata['articles']);
		$userdata['voicetype'] = explode(",", $userdata['voicetype']);
		foreach ($userdata['voicetype'] as $key => &$value) {
			$value = substr(substr($value,1),0,-1);
		}
		$labeldata = M("label")->select();//读取配置
		// p($userdata['voicetype']);exit();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$failtxtsetdata = M("dismiss")->select();
		$uservoice = $db_data->where(array('uuid'=>$userdata['uuid']))->find();
		$userdata['invitename'] = "无";
		$invite = M("invite")->where(array('uuid'=>$userdata['uuid']))->find();
		$userdata['inviteid'] = M("invite")->where(array('uuid'=>$userdata['uuid']))->find()['inviteid'];
		if ($userdata['inviteid']) {
			$userdata['invitename'] = M("member")->where(array('uuid'=>$userdata['inviteid']))->find()['nickname'];
		}else{
			$userdata['inviteid'] = "无";
		}
			// p($leveldata);exit();
		$this->agelabeldata = M("userlabel")->where(array("type"=>1))->select();
		$this->feellabeldata = M("userlabel")->where(array("type"=>2))->select();
		$this->stylelabeldata = M("userlabel")->where(array("type"=>3))->select();
		$this->assign('userdata',$userdata);
		$this->assign('uservoice',$uservoice);
		$this->assign('labeldata',$labeldata);
		$this->assign('leveldata',$leveldata);
		$this->assign('failtxtsetdata',$failtxtsetdata);
		$this->display();
	}

	//注册通过审核
	public function audit_success()
	{
		// p($_POST);exit();
		$uuid = I('post.uuid');
		$data['name'] = I('post.name');
		$data['nickname'] = I('post.nickname');
		$data['username'] = I('post.username');
		$data['sex'] = I('post.sex');
		$data['birthdate'] = I('post.birthdate');
		$data['city'] = I('post.city');
		$data['education'] = I('post.education');
		$data['english'] = I('post.english');
		$data['profession'] = I('post.profession');
		$articles = I('post.articles');
		$agelabel = I('post.agelabel');
		$feellabel = I('post.feellabel');
		$stylelabel = I('post.stylelabel');
		$p = I('post.p');
		$audiotype = I('post.audiotype');

		$data['articles'] = implode(",", $articles);
		/*$voicetype = I('post.voicetype');
		foreach ($voicetype as $key => &$value) {
			$value = "-".$value."-";
		}
		$data['voicetype'] = implode(",", $voicetype);*/
		if ($agelabel) {
			$data['agelabel'] = $this->Parameterimplode($agelabel);
		}
		if ($feellabel) {
			$data['feellabel'] = $this->Parameterimplode($feellabel);
		}
		if ($stylelabel) {
			$data['stylelabel'] = $this->Parameterimplode($stylelabel);
		}

		$soundtype = I('post.soundtype');
		foreach ($soundtype as $key => &$value) {
			$soundvalue[] = $key;
		}
		$data['soundtype'] = implode(",", $soundvalue);
		$data['level'] = I('post.level');
		$submit_type = I('post.submit_type');
		if (!empty($submit_type)) {
			$data['status'] = 9;
		}
		$soundtype['5']?$data['is_introduction']=2:$data2['is_introduction']=3;
		M("member")->where(array('uuid'=>$uuid))->save($data);//更新数据

		$soundtype['0']?$data2['describe_audio_type']=1:$data2['describe_audio_type']=0;
		$soundtype['1']?$data2['emotion_audio_type']=1:$data2['emotion_audio_type']=0;
		$soundtype['2']?$data2['variety_audio_type']=1:$data2['variety_audio_type']=0;
		$soundtype['3']?$data2['news_audio_type']=1:$data2['news_audio_type']=0;
		$soundtype['4']?$data2['english_audio_type']=1:$data2['english_audio_type']=0;
		$soundtype['5']?$data2['introduction_audio_type']=1:$data2['introduction_audio_type']=0;
		// p($data2);exit();
		M("data")->where(array('uuid'=>$uuid))->save($data2);//更新数据

		$soundtype['0']?"":$this->add_verify_log($uuid,"",1);
		$soundtype['1']?"":$this->add_verify_log($uuid,"",2);
		$soundtype['2']?"":$this->add_verify_log($uuid,"",3);
		$soundtype['3']?"":$this->add_verify_log($uuid,"",4);
		$soundtype['5']?"":$this->add_verify_log($uuid,"",9);
		// $this->memberAlidayu($data['username'],'SMS_133445013',$uuid);
		$mobile = $data['username'];
		$type = 1;
		$ypdata['uuid'] = $uuid;
		D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
		// yunpian_send($data['username'],$uuid);
		$res = M("invite")->where(array('uuid'=>$uuid))->order("id desc")->save(array('type'=>1));
		$invite = M("invite")->where(array('uuid'=>$uuid))->order("id desc")->find();
		if ($res&&$invite) {
			$inviteinfo = M("member")->where(array('uuid'=>$invite['inviteid']))->find();
			if ($inviteinfo['score']<100) {
				$num = 2;
				if ($inviteinfo['score']>98) {$num = 1;}
				M("member")->where(array('uuid'=>$invite['inviteid']))->setInc('score',$num);// 加信用分
				M('score')->add(array("uuid"=>$invite['inviteid'],"score"=>$num,"time"=>time(),"describe"=>"邀请成功加分","type"=>1));
			}
			// $this->inviteAlidayu($inviteinfo['username'],'SMS_133560048',$data['nickname'],$inviteinfo['score']);
			$mobile = $inviteinfo['username'];
			$type = 11;
			$ypdata['nickname'] = $data['nickname'];
			D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
			// M("member")->where(array("uuid"=>$invite['inviteid']))->setInc('income',30);
			// M("member")->where(array("uuid"=>$invite['inviteid']))->setInc('balance',30);
			// M('income')->where(array('inviteid'=>$invite['id'],'uuid'=>$invite['inviteid']))->save(array('status'=>1));
		}
		$this->adminlog("审核通过".$uuid);
		$this->success('审核通过',"/Home/Audit/audit_list?p=".$p."&audiotype=".$audiotype,1);

	}
	//注册驳回审核
	public function fail_audit()
	{
		// p($_POST);exit();
		$data['username'] = I('post.telphone');
		$data['message'] = I('post.fail_message');
		$p = I('post.p');
		$audiotype = I('post.audiotype');
		$data['datetime'] = time();
		M('check')->add($data);//添加驳回记录
		$uuid = I('post.user_id');
		$data2['status'] = 5;
		M("member")->where(array('uuid'=>$uuid))->save($data2);//更新数据
		M('broadcasttime')->where(array('uuid'=>$uuid))->delete();
		$data2['describe_audio_type']=0;
		$data2['emotion_audio_type']=0;
		$data2['variety_audio_type']=0;
		$data2['news_audio_type']=0;
		$data2['english_audio_type']=0;
		$data2['introduction_audio_type']=0;
		M("data")->where(array('uuid'=>$uuid))->save($data2);//更新数据
		$tel = M("member")->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		$mobile = $tel;
		$type = 2;
		$ypdata['message'] = $data['message'];
		D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
		// $this->memberAlidayu($tel,'SMS_75895079',$data['message']);
		$this->adminlog("审核驳回".$uuid);
		$this->add_verify_log($uuid,$data['message'],1);
		$this->success('驳回成功',"/Home/Audit/audit_list?p=".$p."&audiotype=".$audiotype,1);

	}


	//再次提交待审核列表
	public function auditagain_list()
	{
		$this->p = I('get.p')?I('get.p'):1;
		$db_member = M("member m");
		$db_data = M("data");
		$where = "status=9 and (emotion_audio_type=2 or variety_audio_type=2 or news_audio_type=2 or introduction_audio_type=2) ";
		$count = $db_member->join('__DATA__ d ON m.uuid = d.uuid')->where($where)->field('m.*')->count();
		$p = getpage($count, 20);
		$userinfo = $db_member->join('__DATA__ d ON m.uuid = d.uuid')->where($where)->field('m.*')->limit($p->firstRow, $p->listRows)->order("creationtime")->select();
		// p($userinfo);exit();
		$this->assign('userinfo',$userinfo);
		$this->assign('page', $p->show());
		$this->display();
	}

	//再次提交待审核详情
	public function auditagain_message()
	{
		$uid = I('get.uid');
		$this->p = I('get.p');
		$this->audiotype = I('get.audiotype');
		$db_member = M("member");
		$db_broadcasttime = M("broadcasttime");
		$db_data = M("data");
		$userdata = $db_member->where(array('status'=>9,'id'=>$uid))->find();
		$userdata['articles'] = explode(",", $userdata['articles']);
		$userdata['voicetype'] = explode(",", $userdata['voicetype']);
		foreach ($userdata['voicetype'] as $key => &$value) {
			$value = substr(substr($value,1),0,-1);
		}
		$labeldata = M("label")->select();//读取配置
		// p($userdata['voicetype']);exit();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$failtxtsetdata = M("dismiss")->select();
		$uservoice = $db_data->where(array('uuid'=>$userdata['uuid']))->find();
			// p($leveldata);exit();
		$this->assign('userdata',$userdata);
		$this->assign('uservoice',$uservoice);
		$this->assign('failtxtsetdata',$failtxtsetdata);
		$this->display();
	}

	//再次提交通过审核
	public function auditagain_success()
	{
		// p($_POST);exit();
		$uuid = I('post.uuid');
		$p = I('post.p');
		$soundtype = I('post.soundtype');
		foreach ($soundtype as $key => &$value) {
			$soundvalue[] = $key;
		}
		$data['soundtype'] = implode(",", $soundvalue);
		$soundtype['5']?$data['is_introduction']=2:$data2['is_introduction']=3;
		M("member")->where(array('uuid'=>$uuid))->save($data);//更新数据
		$soundtype['0']?$data2['describe_audio_type']=1:$data2['describe_audio_type']=0;
		$soundtype['1']?$data2['emotion_audio_type']=1:$data2['emotion_audio_type']=0;
		$soundtype['2']?$data2['variety_audio_type']=1:$data2['variety_audio_type']=0;
		$soundtype['3']?$data2['news_audio_type']=1:$data2['news_audio_type']=0;
		$soundtype['4']?$data2['english_audio_type']=1:$data2['english_audio_type']=0;
		$soundtype['5']?$data2['introduction_audio_type']=1:$data2['introduction_audio_type']=0;
		// p($data2);exit();
		M("data")->where(array('uuid'=>$uuid))->save($data2);//更新数据
		M('audio')->where(['uuid'=>$uuid])->save(['cid'=>$data['soundtype']]);
		$soundtype['0']?"":$this->add_verify_log($uuid,"",1);
		$soundtype['1']?"":$this->add_verify_log($uuid,"",2);
		$soundtype['2']?"":$this->add_verify_log($uuid,"",3);
		$soundtype['3']?"":$this->add_verify_log($uuid,"",4);
		$soundtype['5']?"":$this->add_verify_log($uuid,"",9);
		// $this->memberAlidayu($data['username'],'SMS_133445013',$uuid);
		// yunpian_send($data['username'],$uuid);
		// $this->adminlog("审核通过".$uuid);
		$this->success('审核通过',"/Home/Audit/auditagain_list?p=".$p,1);

	}
	//再次提交驳回审核
	public function fail_auditagain()
	{
		// p($_POST);exit();
		$data['username'] = I('post.telphone');
		$data['message'] = I('post.fail_message');
		$p = I('post.p');
		$audiotype = I('post.audiotype');
		$data['datetime'] = time();
		M('check')->add($data);//添加驳回记录
		$uuid = I('post.user_id');
		$data2['status'] = 5;
		M("member")->where(array('uuid'=>$uuid))->save($data2);//更新数据
		M('broadcasttime')->where(array('uuid'=>$uuid))->delete();
		$tel = M("member")->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		// $this->memberAlidayu($tel,'SMS_75895079',$data['message']);
		$this->adminlog("再次审核".$uuid);
		$this->add_verify_log($uuid,$data['message'],1);
		$this->success('成功',"/Home/Audit/auditagain_list?p=".$p."&audiotype=".$audiotype,1);

	}
	public function memberAlidayu($tel="13091718183",$Code='SMS_72800028',$name)
	{
		$alidayu = new SendSMS();
		$result = $alidayu->send($recNum=$tel,$smsParam='{"name":"'.$name.'"}',$smsTemplateCode=$Code,$smsFreesignName='听说Heard');
		// p($result);
	}
	public function inviteAlidayu($tel="13091718183",$Code='SMS_72800028',$name,$num)
	{
		$alidayu = new SendSMS();
		$result = $alidayu->send($recNum=$tel,$smsParam='{"name":"'.$name.'","num":"'.$num.'"}',$smsTemplateCode=$Code,$smsFreesignName='听说Heard');
		// p($result);
	}



	//认证列表
	public function approve_list()
	{
		$db_authentication = M("authentication a");
		$user_name = I('get.user_name');
		$where = " 1=1 ";
		$this->user_name = $user_name;
		if (!empty($user_name)) {$where .= " and (m.nickname like '%$user_name%' or a.uuid like '%$user_name%' or m.username like '%$user_name%' or a.name like '%$user_name%')";}
		$count = $db_authentication->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->count();
		$p = getpage($count, 20);
		$authenticationinfo = $db_authentication->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->field('a.*,m.nickname,m.username,m.proportion')->limit($p->firstRow, $p->listRows)->select();
		/*foreach ($authenticationinfo as $key => &$value) {
			$userinfo = M('member')->where("uuid=$value[uuid]")->find();
			$value['nickname'] = $userinfo['nickname'];
			$value['username'] = $userinfo['username'];
			$value['proportion'] = $userinfo['proportion'];
		}*/
		// p($authenticationinfo);exit();
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = empty($user_name)?"":$user_name;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->assign('authenticationinfo',$authenticationinfo);
		$this->assign('page', $page);
		$this->display();
	}

	//认证详情
	public function approve_message()
	{
		$did = I('get.did');
		$db_authentication = M("authentication");
		$db_member = M("member");
		$authentication = $db_authentication->where("id=$did")->find();
		$time = explode(",", $authentication['time']);
		$authentication['time'] = implode("点，", $time)."点";
		$member = $db_member->where("uuid=$authentication[uuid]")->find();
		// p($authentication);exit();
		$this->assign('authentication',$authentication);
		$this->assign('member',$member);
		$this->display();
	}

	//通过审核
	public function approve_success()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		$proportion = I('post.proportion');
		if (!is_numeric($proportion)) {
			$this->error('数据异常',"",1);
		}
		M("authentication")->where(array('id'=>$did))->save(array('type'=>1));//更新数据
		M("member")->where(array('uuid'=>$uuid))->save(array('authentication'=>1,'proportion'=>$proportion));//更新数据
		M("member")->where(array('uuid'=>$uuid))->setInc('score',30);// 加信用分
		$tel = M('member')->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		// $this->sendAlidayu($tel,"SMS_118595052");
		$mobile = $tel;
		$type = 5;
		D("Sendsms")->yunpiansend($mobile,$type);
		$this->adminlog("认证通过".$uuid);
		$this->success('认证通过',"/Home/Audit/approve_list",1);
	}

	//修改认证金额
	public function approve_edit()
	{
		$did = I('get.did');
		$db_authentication = M("authentication");
		$db_member = M("member");
		$db_media = M("media");
		$authentication = $db_authentication->where("id=$did")->find();
		$time = explode(",", $authentication['time']);
		$authentication['time'] = implode("点，", $time)."点";
		$member = $db_member->where("uuid=$authentication[uuid]")->find();
		$media = $db_media->where("uuid=$authentication[uuid]")->select();
		$medianame = array();
		foreach ($media as $key => $value) {
			$medianame[$key] = $value['media_name']."(".$value['mmid'].")";
		}
		$medianamearr = implode("，", $medianame);
		$this->assign('authentication',$authentication);
		$this->assign('member',$member);
		$this->assign('medianamearr',$medianamearr);
		$this->display();
	}

	//认证金额修改
	public function edit_approve()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		$proportion = I('post.proportion');
		if (!is_numeric($proportion)) {
			$this->error('数据异常',"",1);
		}
		M("member")->where(array('uuid'=>$uuid))->save(array('proportion'=>$proportion));//更新数据
		$this->adminlog("修改认证信息".$uuid);
		$this->success('修改成功',"/Home/Audit/approve_list",1);
	}
	public function fail_approve()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		// $data['content'] = I('post.approve_message');
		$data['type'] = 2;
		M('authentication')->where(array('id'=>$did))->save($data);
		$tel = M('member')->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		// $this->sendAlidayu($tel,"SMS_124260020");
		$mobile = $tel;
		$type = 6;
		D("Sendsms")->yunpiansend($mobile,$type);
		$this->adminlog("认证未通过".$did);
		$this->success('驳回成功',"/Home/Audit/approve_list",1);

	}
	//待审核列表
	public function failaudit_list()
	{
		$db_member = M("member");
		$db_check = M('check c');
		$user_name = I('get.user_name');
		$this->user_name = $user_name;
		if (!empty($user_name)) {$where = " (m.nickname like '%$user_name%' or m.uuid like '%$user_name%' or m.username like '%$user_name%')";}
		$count = $db_check->where($where)->join('__MEMBER__ m on m.username = c.username')->count();
		$p = getpage($count, 20);
		$userlist = $db_check->where($where)->join('__MEMBER__ m on m.username = c.username')->limit($p->firstRow, $p->listRows)->order("c.id desc")->select();
		foreach ($userlist as $key => &$value) {
			$value['invitename'] = "无";
			if ($value['inviteid']) {
				$value['invitename'] = M("member")->where(array('uuid'=>$value['inviteid']))->find()['nickname'];
			}
		}
		$this->assign('userlist',$userlist);
		$this->assign('page', $p->show());
		$this->display();
	}

	// 邀请人列表
	public function user_invite()
	{
		$uid = I('get.uid');
		$data = M('invite i')
		->join("__MEMBER__ m ON i.uuid = m.uuid")
		->where(array('i.inviteid'=>$uid))
		->select();
		foreach ($data as &$vo) {
			switch ($vo['status']) {
				case 1:
					$vo['status'] = '未完成注册';
					break;
				case 5:
					$vo['status'] = '审核驳回';
					break;
				case 8:
					$vo['status'] = '审核中';
					break;
				case 9:
					$vo['status'] = '通过审核';
					break;
			}
			if ($vo['level'] > 80) {
				$vo['level'] = '(A)'.$vo['level'];
			}else if ($vo['level'] > 50) {
				$vo['level'] = '(B)'.$vo['level'];
			}else if ($vo['level'] > 10) {
				$vo['level'] = '(C)'.$vo['level'];
			}else{
				$vo['level'] = ' ';
			}
			$vo['sex'] = $vo['sex'] == 1 ? '男' : '女';
			$vo['authentication'] = $vo['authentication'] == 1 ? '已认证' : '未认证';
		}
		$this->assign('data',$data);
		$this->display();
	}

/*-审核特殊能力-----------------------------------------------------*/

	//特殊能力列表
	public function specialaudit_list()
	{
		// p($_GET);//exit();
		$type = I('get.type');
		$sort = I('get.sort',0,'intval');
		$this->p = I('get.p')?I('get.p'):1;
		$db_data = M("data a");
		$user_name = I('get.user_name');
		$where = " 1=1 and m.status=9 ";
		switch ($type) {
			case '1':
			switch ($sort) {
				case '1':
					$where .= " and english_audio_type = 1";
					break;
				case '2':
					$where .= " and (english_audio_type = 2 or english_audio_type = 3)";
					break;
				default:
					$where .= " and english_audio_type > 0";
					break;
			}
			$where1 = "english_audio_type = 1";
			$where2 = "(english_audio_type = 2 or english_audio_type = 3)";
			$field = 'a.id,a.uuid,a.english_audio_time time,a.english_audio_type type,m.nickname,m.username';$order="english_audio_time desc";break;
			case '2':
			switch ($sort) {
				case '1':
					$where .= " and bgm_audio_type = 1";
					break;
				case '2':
					$where .= " and (bgm_audio_type = 2 or bgm_audio_type = 3)";
					break;
				default:
					$where .= " and bgm_audio_type > 0";
					break;
			}
			$where1 = "bgm_audio_type = 1";
			$where2 = "(bgm_audio_type = 2 or bgm_audio_type = 3)";
			$field = 'a.id,a.uuid,a.bgm_audio_time time,a.bgm_audio_type type,m.nickname,m.username';$order="bgm_audio_time desc";break;
			case '3':
			switch ($sort) {
				case '1':
					$where .= " and imitate_audio_type = 1";
					break;
				case '2':
					$where .= " and (imitate_audio_type = 2 or imitate_audio_type = 3)";
					break;
				default:
					$where .= " and imitate_audio_type > 0";
					break;
			}
			$where1 = "imitate_audio_type = 1";
			$where2 = "(imitate_audio_type = 2 or imitate_audio_type = 3)";
			$field = 'a.id,a.uuid,a.imitate_audio_time time,a.imitate_audio_type type,m.nickname,m.username';$order="imitate_audio_time desc";break;
		}
		$this->user_name = $user_name;
		$this->type = $type;
		$this->sort = $sort;
		// if (!empty($user_name)) {$where .= " and (a.uuid like '%$user_name%')";}
		if (!empty($user_name)) {$where .= " and (m.nickname like '%$user_name%' or a.uuid like '%$user_name%' or m.username like '%$user_name%')";}
		$count = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->count();
		$p = getpage($count, 20);
		$specialauditlist = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->field($field)->limit($p->firstRow, $p->listRows)->order($order)->select();
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = empty($user_name)?"":$user_name;
		$map['type'] = empty($type)?"":$type;
		$map['sort'] = empty($sort)?"":$sort;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->num1 = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where("m.status=9 and ".$where1)->count();
		$this->num2 = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where("m.status=9 and ".$where2)->count();
		$this->assign('specialauditlist',$specialauditlist);
		$this->assign('page', $page);
		$this->display();
	}

	//特殊能力详情
	public function specialaudit_message()
	{
		$did = I('get.did');
		$type = I('get.type');
		$db_data = M("data a");
		$db_member = M("member");
		$where = "a.id=$did";
		switch ($type) {
			case '1':
			$field = 'a.id,a.uuid,a.english_audio url,a.english_audio_time time,a.english_audio_type type,m.nickname,m.username,a.describe_audio';break;
			case '2':
			$field = 'a.id,a.uuid,a.bgm_audio url,a.bgm_audio_time time,a.bgm_audio_type type,m.nickname,m.username,a.describe_audio';break;
			case '3':
			$field = 'a.id,a.uuid,a.imitate_audio url,a.imitate_audio_time time,a.imitate_audio_type type,m.nickname,m.username,a.describe_audio';break;
		}
		$specialaudit = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->field($field)->find();
		// p($specialaudit);exit();
		$this->type = $type;
		$this->assign('specialaudit',$specialaudit);
		saveUrl();
		$this->display();
	}

	//通过审核
	public function specialaudit_success()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		$type = I('post.type');
		$p = I('post.p');
		switch ($type) {
			case '1':
			$data['english_audio_type'] = 1;
			$name="英文";
			$soundtype = M("member")->field('soundtype')->where(array('uuid'=>$uuid))->find()['soundtype'];
			$userdata['soundtype'] = strpos($soundtype,',4')===false?$soundtype.",4":$soundtype;
			M("member")->where(array('uuid'=>$uuid))->save($userdata);//更新数据
			M('audio')->where(['uuid'=>$uuid])->save(['cid'=>$data['soundtype']]);
				break;
			case '2':
			$data['bgm_audio_type'] = 1;
			$name="添加背景音";
			$memberdata['is_bgm'] = 2;
			M("member")->where(array('uuid'=>$uuid))->save($memberdata);//更新数据
			break;
			case '3':
			$data['imitate_audio_type'] = 1;
			$name="模仿播音";
			break;
		}
		// $tel = M('member')->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		// $this->sendAlidayu($tel,"SMS_118595052");
		M("data")->where(array('id'=>$did))->save($data);//更新数据
		$this->adminlog($uuid."的".$name."能力审核通过".$did);
		$tel = M("member")->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		$mobile = $tel;
		$type = 3;
		$ypdata['name'] = $name;
		D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
		// $this->memberAlidayu($tel,'SMS_124250019',$name);
		// echo session('returnUrl');exit();
		$returnUrl = session('returnUrl')?session('returnUrl'):"/Home/Audit/specialaudit_list?type=".$type."&p=".$p;
		$this->success('审核通过',$returnUrl,1);
	}
// 驳回
	public function fail_specialaudit()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		$type = I('post.type');
		$p = I('post.p');
		switch ($type) {
			case '1':
			$data['english_audio_type'] = 0;
			$name="英文";
			$soundtype = M("member")->field('soundtype')->where(array('uuid'=>$uuid))->find()['soundtype'];
			$userdata['soundtype'] = strpos($soundtype,',4')===false?$soundtype:str_replace(",4","",$soundtype);
			M("member")->where(array('uuid'=>$uuid))->save($userdata);//更新数据
			$status = 5;
			break;
			case '2':
			$data['bgm_audio_type'] = 0;
			$name="添加背景音";
			$memberdata['is_bgm'] = 3;
			M("member")->where(array('uuid'=>$uuid))->save($memberdata);//更新数据
			$status = 6;
			break;
			case '3':
			$data['imitate_audio_type'] = 0;
			$name="模仿播音";
			$status = 7;
			break;
		}
		$message = I('post.specialaudit_message');
		M('data')->where(array('id'=>$did))->save($data);
		$this->adminlog($uuid."的".$name."能力审核未通过".$did."原因：".$message);
		$tel = M("member")->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		M('check')->add(array("username"=>$tel,"message"=>$message,"datetime"=>time(),"type"=>$type));//添加驳回记录
		// $this->failspecialauditAlidayu($tel,'SMS_124250017',$name,$message);
		$mobile = $tel;
		$type = 4;
		$ypdata['name'] = $name;
		$ypdata['message'] = $message;
		D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
		$this->add_verify_log($uuid,$message,$status);
		$returnUrl = session('returnUrl')?session('returnUrl'):"/Home/Audit/specialaudit_list?type=".$type."&p=".$p;
		$this->success('驳回成功',$returnUrl,1);

	}
	public function failspecialauditAlidayu($tel="13091718183",$Code='SMS_124250017',$name,$message)
	{
		$alidayu = new SendSMS();
		$result = $alidayu->send($recNum=$tel,$smsParam='{"name":"'.$name.'","message":"'.$message.'"}',$smsTemplateCode=$Code,$smsFreesignName='听说Heard');
		// p($result);
	}

	//派单认证列表
	public function sendaudit_list()
	{
		// p($_GET);//exit();
		$sort = I('get.sort',0,'intval');
		$this->p = I('get.p')?I('get.p'):1;
		$db_data = M("data a");
		$user_name = I('get.user_name');
		$where = " 1=1 ";

		switch ($sort) {
			case '1':
				$where .= " and short_audio_type = 1";
				break;
			case '2':
				$where .= " and (short_audio_type = 2 or short_audio_type = 3)";
				break;
			default:
				$where .= " and short_audio_type > 0";
				break;
		}
		$where1 = "short_audio_type = 1";
		$where2 = "(short_audio_type = 2 or short_audio_type = 3)";
		$field = 'a.id,a.uuid,a.short_audio_time time,a.short_audio_type type,m.nickname,m.username,m.sex';
		$order="short_audio_time desc";
		$this->user_name = $user_name;
		$this->sort = $sort;
		if (!empty($user_name)) {$where .= " and (m.nickname like '%$user_name%' or a.uuid like '%$user_name%' or m.username like '%$user_name%')";}
		$count = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->count();
		$p = getpage($count, 20);
		$sendauditlist = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->field($field)->limit($p->firstRow, $p->listRows)->order($order)->select();
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = empty($user_name)?"":$user_name;
		$map['sort'] = empty($sort)?"":$sort;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->num1 = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where1)->count();
		$this->num2 = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where2)->count();
		$this->assign('sendauditlist',$sendauditlist);
		$this->assign('page', $page);
		$this->display();
	}

	//认证详情
	public function sendaudit_message()
	{
		$did = I('get.did');
		$db_data = M("data a");
		$db_member = M("member");
		$where = "a.id=$did";
		$field = 'a.id,a.uuid,a.short_audio url,a.short_audio_time time,a.short_audio_type type,m.nickname,m.username,a.describe_audio';
		$sendaudit = $db_data->join('__MEMBER__ m ON a.uuid = m.uuid')->where($where)->field($field)->find();
		// p($sendaudit);exit();
		$this->assign('sendaudit',$sendaudit);
		$this->display();
	}

	//通过审核
	public function sendaudit_success()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		$p = I('post.p');
		$data['short_audio_type'] = 1;
		$name="专项试音";
		M("data")->where(array('id'=>$did))->save($data);//更新数据
		$senddata = M("sendmember")->where(array('uuid'=>$uuid))->find();
		if (empty($senddata)) {
			M("sendmember")->add(array("uuid"=>$uuid));
		}
		$memberdata['is_distribute'] = 2;
		M("member")->where(array('uuid'=>$uuid))->save($memberdata);//更新数据
		$this->adminlog($uuid."的".$name."能力审核通过".$did);
		$tel = M("member")->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		M('sendmember')->where(array('uuid'=>$uuid))->save(array("status"=>1));
		// $this->sendAlidayu($tel,'SMS_133540026');
		$mobile = $tel;
		$type = 3;
		$ypdata['name'] = $name;
		D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
		$this->success('审核通过',"/Home/Audit/sendaudit_list?p=".$p,1);
	}
// 驳回
	public function fail_sendaudit()
	{
		// p($_POST);exit();
		$did = I('post.did');
		$uuid = I('post.uuid');
		$p = I('post.p');
		$data['short_audio_type'] = 0;
		$name="专项试音";
		$message = I('post.sendaudit_message');
		M('data')->where(array('id'=>$did))->save($data);
		M('sendmember')->where(array('uuid'=>$uuid))->save(array("status"=>0));
		$memberdata['is_distribute'] = 3;
		M("member")->where(array('uuid'=>$uuid))->save($memberdata);//更新数据
		$this->adminlog($uuid."的".$name."能力审核未通过".$did."原因：".$message);
		$tel = M("member")->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		// M('check')->add(array("username"=>$tel,"message"=>$message,"datetime"=>time(),"type"=>4));//添加驳回记录
		// $this->failsendauditAlidayu($tel,'SMS_133540025',$message);
		$mobile = $tel;
		$type = 4;
		$ypdata['name'] = $name;
		$ypdata['message'] = $message;
		D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
		$this->add_verify_log($uuid,$message,8);
		$this->success('驳回成功',"/Home/Audit/sendaudit_list?p=".$p,1);

	}
	public function failsendauditAlidayu($tel="13091718183",$Code='SMS_124250017',$reason)
	{
		$alidayu = new SendSMS();
		$result = $alidayu->send($recNum=$tel,$smsParam='{"reason":"'.$reason.'"}',$smsTemplateCode=$Code,$smsFreesignName='听说Heard');
		// p($result);
	}

	//已通过的认证驳回
	public function dismissal()
	{
		$did = I('post.did');
		$uuid = I('post.uuid');
		$message = I('post.approve_message');
		$data['type'] = 2;
		M('authentication')->where(array('id'=>$did))->save($data);
		$tel = M('member')->field('username')->where(array('uuid'=>$uuid))->find()['username'];
		M('check')->add(array("username"=>$tel,"message"=>$message,"datetime"=>time(),"type"=>5));//添加驳回记录
		$save['authentication'] = 0;
		$res = M('member')->where(['uuid'=>$uuid])->save($save);
		$this->add_verify_log($uuid,$message,1);
		$this->adminlog("认证驳回".$did);
//		if($res){
//			M()->commit();
//		}else{
//			M()->rollback();
//		}
		$mobile = $tel;
		$type = 15;
		D("Sendsms")->yunpiansend($mobile,$type);
		$this->success('驳回成功',"/Home/Audit/approve_list",1);
	}
}